package youleche.com.controler.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BHistoryInfo;
import youleche.com.business.BLogInfo;
import youleche.com.caches.CacheFactory;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MBaseParamInfo;
import youleche.com.model.MHistoryInfo;
import youleche.com.model.MLogInfo;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SHistoryInfoServlet
 */
public class SHistoryInfoServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SHistoryInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//新增
				this.addHistoryInfo(request, response);
				break;
			case 1:
				//查询列表
				this.getHistoryInfoList(request, response);
				break;
			case 2:
				//查询列表总条数
				this.getHistoryInfoCount(request, response);
				break;
			case 3:
				//根据ID删除版本信息
				this.deleteHistoryInfoById(request, response);
				break;
		}
	}
	
	/**
	 * 根据ID删除版本信息
	 * @param request 请求
	 * @param response 响应
	 */
	private void deleteHistoryInfoById(HttpServletRequest request, HttpServletResponse response){
		BHistoryInfo handler = new BHistoryInfo();
		try {
			super.response(response, ""+handler.deleteHistoryInfoById(request.getParameter("id")));
		} catch (Exception e) {
			super.response(response, "0");
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查询列表
	 * @param request 请求
	 * @param response 响应
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	private void getHistoryInfoList(HttpServletRequest request, HttpServletResponse response){
		try{
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			BHistoryInfo handler = new BHistoryInfo();
			List<MHistoryInfo> list = handler.getHistoryList(startIndex.toString(), ((Integer)pageSize).toString());
			JsonHelper.printListToJsonString(response, list);
		}catch (Exception e) {
			MLLogFactory.exceptionLog("得到历程列表出错", "得到历程列表出错", e.getMessage(), "127.0.0.1");
			super.response(response, "");
		}
	}
	
	/**
	 * 查询总数
	 * @param request 请求
	 * @param response 响应
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	private void getHistoryInfoCount(HttpServletRequest request, HttpServletResponse response){
		BHistoryInfo handler = new BHistoryInfo();
		try {
			super.response(response, handler.getHistoryListCount());
		} catch (Exception e) {
			super.response(response, "0");
			e.printStackTrace();
		}
	}
		
	/**
	 * 新增
	 * @param request 请求
	 * @param response 响应
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	private void addHistoryInfo(HttpServletRequest request, HttpServletResponse response){
		MHistoryInfo historyInfo = (MHistoryInfo) JsonHelper.getObjectByJsonStringAndGson(request, MHistoryInfo.class);
		BHistoryInfo handler = new BHistoryInfo();
		try {
			if(handler.addHistoryInfo(historyInfo) >0){
				super.response(response, "1");
				return;
			}
		} catch (Exception e) {
			super.response(response, "0");
			e.printStackTrace();
		}
		
		super.response(response, "0");
	}

}
